
#include "CCmdInnerMarket.h"
#include "../mainctrl.h"
#include "../cmd/dataprepare.h"
#include "../cmd/cmdapi.h"

CT_REGISTER_ASYNCCMD(ID_MAIN_INNER_MARKET, CCmdInnerMarket);

int CCmdInnerMarket::Do(void* pData)
{
    if (pData != NULL)
    {
        CT_WARNING(("inner cmd ID_MAIN_INNER_MARKET get data"));
        return Done(RET_OK);
    }

    if (pstMainCtrl == NULL)
    {
        CT_ERROR(("mainctrl point is null"));
        return Done(RET_SYSTEM_FAIL);
    }

    if (0 == uiGID)
    {
        CTSizeString<USER_TOKEN_LEN> stUserToken;
        stUserToken.Set("%s", stHttpReqPara.GetValue("openid"));
        int iIndex = pstMainCtrl->m_stUserTokenToGID.HashFind(stUserToken);
        if (iIndex < 0)
        {
            CT_ERROR(("%s", stUserToken()));
            return Done(RET_SYSTEM_FAIL);
        }

        uiGID = pstMainCtrl->m_stUserTokenToGID[iIndex]._uiNowUseGID;
    }

    LogicDo(RET_OK);

    return RET_OK;
}

int CCmdInnerMarket::LogicDo(int iRet)
{
    if (iRet != RET_OK)
    {
        return Done(iRet);
    }

    PREPARE_PLAYER_ONLINE_DATA(this, uiGID);
    PREPARE_PLAYER_DETAIL_EX1_INFO(this, uiGID);

    int iIndex = 0;
    iIndex = pstMainCtrl->m_stPlayerDetailEx1InfoHash.HashFind(uiGID);
    if (iIndex < 0)
    {
        CT_ERROR(("online user %u has no detail ex1 info", uiGID));
        return AnsClients(RET_SYS_ERROR);
    }
    CPlayerDetailInfoEx1 & stPlayerDetailInfoEx1 = pstMainCtrl->m_stPlayerDetailEx1InfoHash[iIndex];

    iIndex = pstMainCtrl->m_stPlayerOnlineDataHash.HashFind(uiGID);
    if (iIndex < 0)
    {
        CT_ERROR(("gid = %u", uiGID));
        return AnsClients(RET_SYS_ERROR);
    }
    CPlayerOnlineData& stPlayerOnlineData = pstMainCtrl->m_stPlayerOnlineDataHash[iIndex];

    CMarketTask & stMarket = stPlayerDetailInfoEx1._stMarketTask;

    //检查任务的ID是否一致
    if (MARKET_OPT_INFORM_FINISH != _ucOpt)
    {
        unsigned int uiTaskId = pstMainCtrl->m_stMarketMgr.GetTaskId(stHttpReqPara.GetValue("contractid"));
        if (stMarket._uiTaskId != uiTaskId)
        {
            iRet = ERROR_MARKET_TASK_NONE;
        }
    }    
    
    CReqApi stApiReq;
    switch (_ucOpt)
    {
    case MARKET_OPT_INFORM_FINISH:
        {
            stMarket._ucHasFinish = true;
            iRet = pstMainCtrl->m_stMarketMgr.GetInfomTaskCompletedPara(stMarket, stPlayerOnlineData, stApiReq);
            if (iRet == RET_OK)
            {
                //创建应答的命令
                CCmdApi* pstCmd = (CCmdApi*)CT_CMDFACTORY->CreateCmd(ID_MAINAPI_TAPI);
                if (!pstCmd)
                {
                    CT_WARNING(("create cmd error"));
                }
                else
                {
                    pstCmd->pstMainCtrl = pstMainCtrl;
                    pstCmd->stReq = stApiReq;
                    pstCmd->Do(NULL);
                }
            }
        }
        break;
    case MARKET_OPT_CHECK_COMPLETION:
        {                           
            iRet = pstMainCtrl->m_stMarketMgr.CheckCompletion(stMarket, stPlayerOnlineData);
            if (RET_OK == iRet)
            {
                _szSendBack.Set("{\"ret\":%d,\"msg\":\"%s\",\"zoneid\":\"%d\"}", iRet, "OK", CONF->ZoneID);
            }
            else
            {
                _szSendBack.Set("{\"ret\":%d,\"msg\":\"%s\"}", iRet, "参数错误");
            }
        }
        break;
    case MARKET_OPT_AWARD_CALLBACK:
        {   
            vector<string> aryAward;
            CTStringUtils::SplitNoSkip(stHttpReqPara.GetValue("payitem"), "*",  aryAward);
            if (2 == aryAward.size())
            {
                unsigned int uiGiftID = atoi(aryAward[0].c_str());
                int iCount = atoi(aryAward[1].c_str());
                iRet = pstMainCtrl->m_stMarketMgr.ProvideAward(stMarket, stPlayerOnlineData, uiGiftID, iCount);
            }
            else
            {
                CT_ERROR(("%d, %s", uiGID, stHttpReqPara.GetValue("payitem")));
                iRet = ERROR_MARKET_TASK_AWARD;
            }

            if (RET_OK == iRet)
            {
                _szSendBack.Set("{\"ret\":%d,\"msg\":\"%s\",\"zoneid\":\"%d\"}", iRet, "OK", CONF->ZoneID);
                pstMainCtrl->m_stNotifyMgr.Notify(uiGID, NOTIFY_TASK_LIST);
            }
            else
            {
                _szSendBack.Set("{\"ret\":%d,\"msg\":\"%s\"}", iRet, "参数错误");
            }            
        }
        break;
    default:
        iRet = ERROR_MARKET_TASK_OPT;
    }

    CVarObjectKey stKey;
    stKey.m_chDataType = DT_PLAYER_DETAIL_EX1_INFO;
    stKey.m_u64ID = uiGID;
    pstMainCtrl->UpdateData(stKey, stPlayerDetailInfoEx1, VOP_UPDATE);

    return AnsClients(iRet);
}

int CCmdInnerMarket::AnsClients (int iRet)
{
    if (_szSendBack.GetDataLen() > 0)
    {
        pstMainCtrl->m_stEncodeBuf.Clear();
        memcpy(pstMainCtrl->m_stEncodeBuf.GetFreeBuf(), _szSendBack(), _szSendBack.GetDataLen());
        pstMainCtrl->m_stEncodeBuf.Append(_szSendBack.GetDataLen());

        CT_TRACE(("CCmdInnerMarket::AnsClients %s", pstMainCtrl->m_stEncodeBuf.GetUsedBuf()));

        if (pstMainCtrl->AnswerToClient(stHead, pstMainCtrl->m_stEncodeBuf, true))
        {
            return Done(RET_SYS_ERROR);
        }
    }

    return Done(iRet);
}
